/**
 * Created with JetBrains WebStorm.
 * User: renji
 * Date: 14/5/13
 * Time: 1:51 PM
 * To change this template use File | Settings | File Templates.
 */

describe("AuthoriseTransactionConfirmController unit test", function() {
    var authTransactionConfirmController = null;
    var objE = {errorCode:'code', errorMessage:'message'};
    var transfer = null;
    beforeEach(function() {
        var t_alertCtrl = App.AlertController.create();
        var t_loadingCtrl = App.LoadingController.create();

        transfer = App.OneTimeBillPayment.create();

        App.AuthoriseTransactionConfirmController.reopen({
            init:function(){
                this.set('controllers.alert', t_alertCtrl);
                this.set('controllers.loading', t_loadingCtrl);
                this.set('transfer', transfer);
            }
        });
        spyOn(App.getSession(),'getCurrentTransfer').andReturn(transfer);
        authTransactionConfirmController = App.AuthoriseTransactionConfirmController.create();
    });

    it("continueAction", function() {
        var transferResult = [];
        spyOn(authTransactionConfirmController,'validateOTP');
        spyOn(authTransactionConfirmController, 'confirmTransactionCallback');
        spyOn(App.api_authoriseTransaction,'confirmTransaction').andCallFake(function(transferResult, callback) {
            callback(transferResult,objE);
        });

        authTransactionConfirmController.continueAction();
        expect(authTransactionConfirmController.validateOTP).toHaveBeenCalledWith('',10);
        expect(App.api_authoriseTransaction.confirmTransaction).toHaveBeenCalled();
        expect(authTransactionConfirmController.confirmTransactionCallback).toHaveBeenCalled();
    });

    it("continueAction with exception", function(){
        authTransactionConfirmController.continueAction();
    });


    it("confirmTransactionCallback - Success", function(){

        var objE1 =  BreezeException.create({
            errorCode:'code',
            errorMessage: 'Error Messge'
        });

        spyOn(authTransactionConfirmController, 'hasError').andReturn(false);
        spyOn(authTransactionConfirmController, 'hideLoading');
        spyOn(authTransactionConfirmController, 'transitionTo');



        authTransactionConfirmController.confirmTransactionCallback([]);

        expect(authTransactionConfirmController.transitionTo).toHaveBeenCalledWith('authorise.transactionSummary');
        expect(authTransactionConfirmController.hideLoading).toHaveBeenCalled();
    });

    it("confirmTransactionCallback - Other error", function(){

        var objE1 =  BreezeException.create({
            errorCode:'code',
            errorMessage: 'Error Messge'
        });

        spyOn(authTransactionConfirmController.get('controllers').get('alert'), 'showAlert').andCallFake(function(message, callback){
            callback();
        });

        spyOn(authTransactionConfirmController, 'transitionTo');
        spyOn(authTransactionConfirmController, 'hideLoading');

        authTransactionConfirmController.confirmTransactionCallback([], objE1);

        expect(authTransactionConfirmController.hideLoading).toHaveBeenCalled();
        expect(authTransactionConfirmController.get('controllers').get('alert').showAlert).toHaveBeenCalledWith(objE1.getCustomErrorMessage('GENERAL_EXCEPTION'), jasmine.any(Function));
        expect(authTransactionConfirmController.transitionTo).toHaveBeenCalledWith('authorise.transactions', {needsRefresh: true});

    });

    it("confirmTransactionCallback - ERR_ETAC_VALIDATION_FAILED", function(){

        var objE1 =  BreezeException.create({
            errorCode:'ERR_ETAC_VALIDATION_FAILED',
            errorMessage: 'Error Messge'
        });

        spyOn(authTransactionConfirmController, 'hasError').andReturn(true);
        spyOn(authTransactionConfirmController, 'hideLoading');

        authTransactionConfirmController.confirmTransactionCallback([], objE1);

        expect(authTransactionConfirmController.hasError).toHaveBeenCalledWith(objE1);

        expect(authTransactionConfirmController.hideLoading).toHaveBeenCalled();
    });

});
